﻿using Abp.Web.Models;
using Abp.Web.Security.AntiForgery;
using KDS.Mes.Mes.Reports_Offline.Dtos;
using KDS.Mes.Mes.Reports_Report;
using KDS.Mes.Mes.Reports_Report.Dtos;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class Reports_OfflineController : MesControllerBase
    {
        private readonly IReports_OfflineAppService _reports_ReportAppService;
        static List<Reports_OfflineListDto> newObj;

        public Reports_OfflineController(IReports_OfflineAppService reports_OfflineAppService)
        {
            _reports_ReportAppService = reports_OfflineAppService;

        }
        // GET: Retrospects
        public ActionResult Index()
        {
            return View();
        }

        [DontWrapResult]
        public JsonResult GetOffline(GetReports_OfflineInput input)
        {
            newObj = null;
            newObj = _reports_ReportAppService.GetReports_Offline(input);
            return Json(newObj, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        [DisableAbpAntiForgeryTokenValidation]
        public void Print()
        {
            NpoiExcel("test");

        }
        public void NpoiExcel(string title)
        {
            string TempletFileName = Server.MapPath("~") + "/test/test.xlsx";
            //导出文件  
            string ReportFileName = Server.MapPath("out.xlsx");
            FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
            XSSFWorkbook hssfworkbook = new XSSFWorkbook(file);


            NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheet("Sheet1");
            NPOI.SS.UserModel.IRow headerrow = sheet.CreateRow(0);
            ICellStyle style = hssfworkbook.CreateCellStyle();
            style.Alignment = HorizontalAlignment.Center;
            style.VerticalAlignment = VerticalAlignment.Center;
            sheet.GetRow(1).GetCell(0).SetCellValue("线体名称");
            sheet.GetRow(1).GetCell(1).SetCellValue("座椅型号");
            sheet.GetRow(1).GetCell(2).SetCellValue("上线数量");
            sheet.GetRow(1).GetCell(3).SetCellValue("下线数量");
            sheet.GetRow(1).GetCell(4).SetCellValue("在线数量");

            int iRowIndex = 2;
            int iCellIndex = 0;
            foreach (Reports_OfflineListDto m in newObj)
            {
                if (m.LineId == 1)
                {
                    sheet.GetRow(iRowIndex).GetCell(0).SetCellValue("X001");
                }
                if (m.LineId == 2)
                {
                    sheet.GetRow(iRowIndex).GetCell(0).SetCellValue("X002");
                }
                if (m.LineId == 3)
                {
                    sheet.GetRow(iRowIndex).GetCell(0).SetCellValue("X003");
                }
                sheet.GetRow(iRowIndex).GetCell(1).SetCellValue(m.ALC.ToString());
                sheet.GetRow(iRowIndex).GetCell(2).SetCellValue(m.sum.ToString());
                sheet.GetRow(iRowIndex).GetCell(3).SetCellValue(m.offline);
                if (m.offline != null &&m.offline!="")
                {
                    sheet.GetRow(iRowIndex).GetCell(4).SetCellValue((int.Parse(m.sum) - int.Parse(m.offline)).ToString());
                }
               
                iRowIndex++;

            }
            sheet.ForceFormulaRecalculation = true;
            MemoryStream ms = new MemoryStream();
            hssfworkbook.Write(ms);
            //Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx",HttpUtility.UrlEncode(title + "_" + DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8)));
            System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", title + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx"));
            Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
            Response.BinaryWrite(ms.ToArray());

            Response.Flush();
            System.Web.HttpContext.Current.Response.End();

            Response.End();
            hssfworkbook = null;
            ms.Close();
            ms.Dispose();
        }
    }
}